package Effects;

import View.MainScreen;
import java.awt.Color;
import java.awt.GraphicsConfiguration;
import java.awt.Rectangle;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.plaf.basic.BasicInternalFrameUI;
import sun.java2d.SunGraphicsEnvironment;

public class Adjust {
    
    private Connection conn;
    private String query;
    private PreparedStatement stmt;
    private ResultSet rs;
    int random;
    
    public static void removerDecoreInternelFrame(JInternalFrame internalFrame){
        internalFrame.setVisible(false);
        ((BasicInternalFrameUI) internalFrame.getUI()).setNorthPane(null);
        internalFrame.setBorder(null);
    }
    
    public static void startMainScreen(){        
        MainScreen pr = new MainScreen();
        GraphicsConfiguration config = pr.getGraphicsConfiguration();
        Rectangle usableBounds = SunGraphicsEnvironment.getUsableBounds(config.getDevice());
        pr.setMaximizedBounds(new Rectangle(0, 0, usableBounds.width, usableBounds.height));
        pr.setExtendedState((pr.getExtendedState() & MainScreen.MAXIMIZED_BOTH) == 
                MainScreen.MAXIMIZED_BOTH ? JFrame.NORMAL : JFrame.MAXIMIZED_BOTH); 
        pr.getContentPane().setBackground(Color.white); 
        pr.pack();
        pr.show();
    }
    
    public boolean verifyRandom() throws SQLException { 
        random = (new Random().nextInt(999999));
        conn = LinkDB.LinkDB.getConexao();
        query = "SELECT protocolo FROM os WHERE protocolo = ?";
        stmt = conn.prepareStatement(query);
        stmt.setInt(1, random);
        rs = stmt.executeQuery();
        if (rs.next()) {
            return true;
        } else {
            return false;
        }
    }

    public void setProtocoloOs() throws SQLException {
        while (verifyRandom() == true) {
            verifyRandom();
        }
        MainScreen.protocolo.setText(String.valueOf(random));        
    }
    
    public void clearScreen(){
        MainScreen.protocolo.setText("");
        MainScreen.empresa.setText("");
        MainScreen.endereco.setText("");
        MainScreen.contato.setText("");
        MainScreen.setor.setText("");
        MainScreen.telefone.setText("");
        MainScreen.telefone2.setText("");
        MainScreen.equipamento.setSelectedItem("Adicionar");
        MainScreen.defeito.setText("");
        MainScreen.solucao.setText("");
        MainScreen.urgente.isOpaque();
        MainScreen.resolvido.isSelected();        
    }
    
}
